package com.qipay.admin.auth.controller;

import com.qipay.admin.BaseController;
import com.qipay.admin.auth.vo.UserVo;
import com.qipay.admin.cp.vo.CpChannelPostDTO;
import com.qipay.admin.cp.vo.CpInfoDTO;
import com.qipay.admin.cp.vo.CpPostDTO;
import com.qipay.admin.login.AuthPassport;
import com.qipay.admin.tags.PageModel;
import com.qipay.admin.utils.CpInfoBuilder;
import com.qipay.admin.utils.SiteHelper;
import com.qipay.baas.model.*;
import com.qipay.channelType.ChannelTypeService;
import com.qipay.cp.CpService;
import com.qipay.cpaccount.CpAccountService;
import com.qipay.cpchannel.service.CpChannelService;
import com.qipay.cpinfo.service.CpInfoService;
import com.qipay.ips.service.IpsService;
import com.qipay.order.service.OrderService;
import com.qipay.security.UserDetailsServiceImpl;
import com.qipay.user.entity.UserLevel;
import com.qipay.user.service.UserService;
import com.qipay.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.constraints.NotNull;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class AuthController extends BaseController {

    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    UserService userService;


    @RequestMapping(value = {"/auth/test"}, method = RequestMethod.GET)
    public ModelAndView addUser() throws UnsupportedEncodingException {
        ModelAndView view = createModelAndView("test-js");
        view.addObject("level",UserLevel.ADMIN);
        return view;
    }

    public static void main(String[] args) {
        AuthController authController = new AuthController();
        MathOperation addition = (int a, int b) -> a + b;

    }

    interface MathOperation {
        int operation(int a, int b);
    }

    interface GreetingService {
        void sayMessage(String message);
    }

    private int operate(int a, int b, MathOperation mathOperation){
        return mathOperation.operation(a, b);
    }
    /**
     * 跳转添加管理员
     * @param loginUser
     * @return
     * @throws UnsupportedEncodingException
     */
    @AuthPassport(key = "admin")
    @RequestMapping(value = {"/auth/addUser"}, method = RequestMethod.GET)
    public ModelAndView addUser(@AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser
        ) throws UnsupportedEncodingException {
        ModelAndView view = createModelAndView("add-user");
        view.addObject("level",UserLevel.ADMIN);
        return view;
    }
    /**
     * 添加管理员
     * @param loginUser
     * @return
     * @throws UnsupportedEncodingException
     */
    @AuthPassport(key = "admin")
    @RequestMapping(value = {"/auth/saveUser"}, method = RequestMethod.POST)
    public ModelAndView saveUser(@AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser
                                 ,UserVo userVo
    ){
        User user = null;
        if(userVo.getId()!=null){
            user = userService.getByPK(userVo.getId());
        }else{
            user = userService.getByName(userVo.getName());
            if (user!=null){
                ModelAndView view = createModelAndView("add-user");
                view.addObject("level",UserLevel.ADMIN);
                view.addObject("msg","名称已存在~");
                return view;
            }
            user = new User();
        }
        user.setName(userVo.getName());
        user.setSex((byte) 1);
        user.setPwd(userVo.getPassword());//默认123456
        //user.setPwd(passwordEncoder.encode(userVo.getPassword()));避免加密解密，这里直接用加密之后的，也不用改了。
        user.setPid( 0);
        user.setAuthCode(userVo.getAuthCode());
        user.setLevel(UserLevel.ADMIN_SON.getCode());
        if(userVo.getId()!=null)userService.updateByPrimaryKey(user);
        else userService.insert(user);

        ModelAndView view = createModelAndView("user-list");
        List<User>users = userService.findByLevel(500);
        view.addObject("users",users);
        view.addObject("level",UserLevel.ADMIN);
        return view;
    }

    /**
     * 添加管理员
     * @param loginUser
     * @return
     * @throws UnsupportedEncodingException
     */
    @AuthPassport(key = "admin")
    @RequestMapping(value = {"/auth/userList"}, method = RequestMethod.GET)
    public ModelAndView userList(@AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser
            ,UserVo userVo
    ){
        List<User>users = userService.findByLevel(500);
        ModelAndView view = createModelAndView("user-list");
        view.addObject("users",users);
        view.addObject("level",UserLevel.ADMIN);
        return view;
    }

    /**
     * 添加管理员
     * @param loginUser
     * @return
     * @throws UnsupportedEncodingException
     */
    @AuthPassport(key = "admin")
    @RequestMapping(value = {"/auth/toUpdate"}, method = RequestMethod.GET)
    public ModelAndView toUpdate(@AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser
            ,String name
    ){
        ModelAndView view = createModelAndView("add-user");
        User user = userService.getByName(name);
        view.addObject("user",user);
        view.addObject("level",UserLevel.ADMIN);
        return view;
    }

}
